package com.bidevalution.business.impl;

import com.bidevalution.business.service.BeSectionParamsService;


import com.bidevalution.dao.mapper.BeSectionParamsMapper;
import com.google.common.collect.Maps;
import com.qtp.auth.api.ParamsService;
import com.qtp.core.util.OConvertUtils;
import com.qtp.user.models.SysParams;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 参数抽取业务接口实现
 * @author shenh@glodon.com
 * @date 2019-05-13 10:29
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BeSectionParamsServiceImpl implements BeSectionParamsService {

    @Autowired
    private BeSectionParamsMapper beSectionParamsMapper;
    @Autowired(required = false)
    private ParamsService paramsService;

    /**
     * 根据标段ID和参数code获取参数值
     * @param sectionId
     * @param paramCode
     * @return
     */
    @Override
    public String getBeSectionParamsByCode(String sectionId, String paramCode) throws Exception {
        //获取第二个数据源
        
        return beSectionParamsMapper.getBeSectionParamsByCode(sectionId, paramCode);
    }

    /**
     * 根据标段ID和参数code获取参数值
     * @param sectionId
     * @param paramCodes
     * @return
     */
    @Override
    public String getBeSectionParamsByCodes(String sectionId, String paramCodes) throws Exception {
        //获取第二个数据源
        
        return beSectionParamsMapper.getBeSectionParamsByCodes(sectionId, paramCodes);
    }

    /**
     * 新增参数
     * @param sectionId
     * @param paramCode
     * @return
     */
    @Override
    public Integer insertBeSectionParams(String sectionId, String paramCode, String paramValue) throws Exception {
        //获取第二个数据源
        
        if(beSectionParamsMapper.getBeSectionParamsByCode(sectionId, paramCode) != null){
            return beSectionParamsMapper.updateBeSectionParams(sectionId, paramCode, paramValue);
        } else {
            return beSectionParamsMapper.insertBeSectionParams(sectionId, paramCode, paramValue);
        }
    }


    /**
     *  获得后台参数配置信息
     * @param patpCode 参数类型
     * @return
     * @throws Exception
     */
    @Override
    public List<SysParams> getSysParams(String patpCode) throws Exception {
        Map<String, Object> paramsMap = Maps.newHashMap();
        paramsMap.put("patpCode",patpCode);
        return this.paramsService.querySysParamsByCode(paramsMap, SysParams.class);
    }

    /**
     * 获得统一登录一条配置信息
     * @param patpCode 参数类型
     * @return
     * @throws Exception
     */
    @Override
    public String getOneSysParamValue(String patpCode) throws Exception {
        try {
            Map<String, Object> params = Maps.newHashMap();
            if(StringUtils.isNotBlank(patpCode)) {
                params.put("patpCode", patpCode);
            }
            List<SysParams> sysParamsList =  this.paramsService.querySysParamsByCode(params, SysParams.class);
            if (sysParamsList != null && sysParamsList.size() > 0 && sysParamsList.get(0) != null) {
                String value = sysParamsList.get(0).getSypaValue();
                if (StringUtils.isNotEmpty(value)) {
                    //获得参数值
                    return value;
                } else {
                    throw new Exception("参数管理中没有相关参数！");
                }
            } else {
                throw new Exception("参数管理中没有相关参数！");
            }
        } catch (Exception e) {
            throw e;
        }
    }

}
